//
//  HWGroupChatRecommendCell.swift
//  HWanGame
//
//  Created by 王昌健 on 2022/10/25.
//  Copyright © 2022 94Wan. All rights reserved.
//

import UIKit

class HWGroupChatRecommendCell: UITableViewCell {
    var icon:UIImageView!
    var nameLabel:UILabel!
    var peopleImg:UIImageView!
    var peopleLabel:UILabel!
    var contentLabel:UILabel!
    var joinButton:UIButton!
    var model:HWGroupChatRecommendModel? {
        didSet {
            icon.kf.setImage(with: URL(string: model?.icon ?? ""))
            nameLabel.text = model?.name
            peopleLabel.text = model?.sum
            contentLabel.text = model?.gonggao
            
            if model?.join == true {
                joinButton.setTitle("进入", for: .normal)
            } else {
                joinButton.setTitle("加群", for: .normal)
            }
        }
    }
    var joinButtonClick:((_:HWGroupChatRecommendModel)->())?
    class func cellWithTableView(_ tableView:UITableView) -> (HWGroupChatRecommendCell) {
        let reUse:String = "HWGroupChatRecommendCell"
        
        var cell = tableView.dequeueReusableCell(withIdentifier: reUse)
        
        if cell == nil {
            cell = HWGroupChatRecommendCell.init(style: UITableViewCell.CellStyle.default, reuseIdentifier: reUse)
        }
        
        return cell as! HWGroupChatRecommendCell
    }
    
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        initUI()
        initFrame()
    }
    
    func initUI() -> Void {
        selectionStyle = UITableViewCell.SelectionStyle.none
        backgroundColor = UIColor.clear
        
        icon = UIImageView()
        icon.layer.cornerRadius = 10*widthScale
        icon.contentMode = .scaleAspectFill
        icon.layer.masksToBounds = true
        contentView.addSubview(icon)
        
        nameLabel = HWLabel.createBoldLabel(title: "", color: "#333333", font: 14*widthScale)
        contentView.addSubview(nameLabel)
        
        peopleImg = UIImageView()
        peopleImg.image = UIImage(named: "groupChatRecommendPeople")
        contentView.addSubview(peopleImg)
        
        peopleLabel = HWLabel.createLabel(title: "", color: "#888888", font: 11*widthScale)
        contentView.addSubview(peopleLabel)
        
        contentLabel = HWLabel.createLabel(title: "", color: "#FA8C08", font: 11*widthScale)
        contentView.addSubview(contentLabel)
        
        joinButton = UIButton(type: .custom)
        joinButton.layer.cornerRadius = 14*widthScale
        joinButton.setTitleColor(.colorWithHexString(hexString: "#1CD67E"), for: .normal)
        joinButton.layer.masksToBounds = true
        joinButton.layer.borderColor = UIColor.colorWithHexString(hexString: "#1CD67E").cgColor
        joinButton.layer.borderWidth = 1*widthScale
        joinButton.titleLabel?.font = .boldSystemFont(ofSize: 13*widthScale)
        joinButton.addTarget(self, action: #selector(joinButtonClick(_:)), for: .touchUpInside)
        contentView.addSubview(joinButton)
    }
    
    func initFrame() -> Void {
        icon.snp.makeConstraints { make in
            make.left.equalTo(18*widthScale)
            make.top.equalTo(9*widthScale).priority(.high)
            make.width.height.equalTo(54*widthScale)
            make.bottom.equalToSuperview()
        }
        
        nameLabel.snp.makeConstraints { make in
            make.left.equalTo(icon.snp.right).offset(9*widthScale)
            make.right.equalTo(joinButton.snp.left).offset(-10*widthScale)
            make.top.equalTo(icon)
            make.height.equalTo(20*widthScale)
        }
        
        peopleImg.snp.makeConstraints { make in
            make.left.equalTo(nameLabel)
            make.top.equalTo(nameLabel.snp.bottom).offset(5*widthScale)
            make.width.height.equalTo(11*widthScale).priority(.high)
        }
        
        peopleLabel.snp.makeConstraints { make in
            make.left.equalTo(peopleImg.snp.right).offset(4*widthScale)
            make.centerY.equalTo(peopleImg)
        }
        
        contentLabel.snp.makeConstraints { make in
            make.left.equalTo(nameLabel)
            make.top.equalTo(peopleImg.snp.bottom).offset(4*widthScale)
            make.right.equalTo(joinButton.snp.left).offset(-10*widthScale)
            make.height.equalTo(15*widthScale)
        }
        
        joinButton.snp.makeConstraints { make in
            make.right.equalTo(-18*widthScale)
            make.centerY.equalTo(icon)
            make.width.equalTo(54*widthScale)
            make.height.equalTo(28*widthScale)
        }
    }
    
    
    @objc func joinButtonClick(_:UIButton) -> Void {
        self.joinButtonClick?(model ?? HWGroupChatRecommendModel())
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
